package com.wbao.dianniu.adapter;

import java.util.ArrayList;
import java.util.List;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.wbao.dianniu.R;
import com.wbao.dianniu.data.ActivityListData;
import com.wbao.dianniu.utils.PictureDownloadUtils;

/**
 * 线下活动
 * 
 * @author yejianjun
 *
 */
public class OffineActivityAdapter extends BaseAdapter {
	private Context mContext;
	private LayoutInflater mInflater;
	private List<ActivityListData> mList;
	
	public OffineActivityAdapter(Context context) {
		mContext = context;
		mInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		if(null == mList)
			mList = new ArrayList<ActivityListData>();
	}
	public List<ActivityListData> getList(){
		return mList;
	}
	public void clearData(){
		if(null !=  mList){
			mList.clear();
		}
	}
	public void addData(List<ActivityListData> list){
		if(null == list)
			return;
		for (int i = 0; i < list.size(); i++) {
			mList.add(list.get(i));
		}
	}
	
//	public void addTopData(ActivityListData data){
//		mList.add(0, data);
//	}
	
	@Override
	public int getCount() {
		if (null == mList)
			return 0;
		else
			return mList.size();
	}

	@Override
	public Object getItem(int position) {
		
		return mList.get(position);
	}

	@Override
	public long getItemId(int position) {
		// TODO Auto-generated method stub
		return 0;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		GridHolder holder= null;
		if (convertView == null ) {
			convertView = mInflater.inflate(R.layout.offine_activity_list_item, null);   
			holder = new GridHolder();
			holder.headIV = (ImageView) convertView.findViewById(R.id.user_head);
			holder.headName = (TextView) convertView.findViewById(R.id.user_name);
			holder.headLabel = (TextView) convertView.findViewById(R.id.user_label);
			holder.subjectTV = (TextView) convertView.findViewById(R.id.offline_subject);
			holder.maxPersionTV = (TextView) convertView.findViewById(R.id.offline_maxPersion);
			holder.startDateTV = (TextView) convertView.findViewById(R.id.offline_startDate);
			holder.priceTV = (TextView) convertView.findViewById(R.id.offline_price);
			holder.addressTV = (TextView) convertView.findViewById(R.id.offline_address);
			holder.browseCountTV = (TextView) convertView.findViewById(R.id.offline_browseCount);
			holder.inCountTV = (TextView) convertView.findViewById(R.id.offline_inCount);
			holder.passCountTV = (TextView) convertView.findViewById(R.id.offline_passCount);
			convertView.setTag(holder);
		}else{
			holder = (GridHolder) convertView.getTag();
		}
		ActivityListData data = mList.get(position);
		if(null != data){
			PictureDownloadUtils.displayHeadImage(mContext,holder.headIV, data.getHeadPic());
			holder.headName.setText(data.getRealName()== null ? "": data.getRealName());
			holder.headLabel.setText(data.getLabel()== null ? "": data.getLabel());
			holder.subjectTV.setText(data.getSubject()== null ? "": data.getSubject());
			holder.maxPersionTV.setText(data.getMaxPersion() +"人");
			holder.startDateTV.setText(data.getStartDate()== null ? "": data.getStartDate());
			holder.priceTV.setText(data.getPrice() + "元/人");
			holder.addressTV.setText(data.getAddress() == null ? "": data.getAddress());
			holder.browseCountTV.setText(mContext.getResources().getString(R.string.browse) + data.getBrowseCount());
			holder.inCountTV.setText(mContext.getResources().getString(R.string.sign_up) + data.getInCount());
			holder.passCountTV.setText(mContext.getResources().getString(R.string.pass) + data.getPassCount());
		}
		
		return convertView;
	}

	private class GridHolder {
		public ImageView headIV;
		public TextView headName;
		public TextView headLabel;
		public TextView subjectTV;
		public TextView maxPersionTV;
		public TextView startDateTV;
		public TextView priceTV;
		public TextView addressTV;
		public TextView browseCountTV;
		public TextView inCountTV;
		public TextView passCountTV;
	}
	
}
